package com.laikan.reader.core.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.socks.library.KLog;

import java.util.ArrayList;

public abstract class MyBaseAdapter<T> extends BaseAdapter {

    public ArrayList<T> mData;
    protected Context mContext;
    protected LayoutInflater mInflater;
    private BaseHolder<T> holder;

    public MyBaseAdapter(Context context, ArrayList<T> datas) {
        mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.mData = datas;
    }

    @Override
    public int getCount() {
        KLog.i("parent--------getCount");
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        KLog.i("parent--------getItem");
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        KLog.i("parent--------getItemId");
        return position;
    }

    public void cleanDatas() {
        if (mData != null && !mData.isEmpty()) {
            mData.clear();
            notifyDataSetChanged();
        }
    }

    public ArrayList<T> getDatas() {
        KLog.i("parent--------getDatas");
        return this.mData;
    }

    public void upDatas(ArrayList<T> datas) {
        KLog.i("parent--------upDatas");
        this.mData = datas;
        notifyDataSetChanged();
    }

    public void addDatas(ArrayList<T> datas) {
        this.mData.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        KLog.i("parent--------getView");
        if (convertView == null) {
            holder = getHolder();
        }else {
            if (convertView.getTag() != null) {
                holder = (BaseHolder<T>)convertView.getTag();
            }
        }
        holder.setData(mData.get(position));
        return holder.getRootView();
    }

    public abstract BaseHolder<T> getHolder();

}
